package com.xyy.saas.payment.controller;

import com.alibaba.fastjson.JSON;
import com.xyy.saas.common.util.ResultVO;
import com.xyy.saas.payment.adpater.fumin.config.AreaCodeManager;
import com.xyy.saas.payment.common.pool.NamedTask;
import com.xyy.saas.payment.common.exception.PaymentException;
import com.xyy.saas.payment.common.pool.PoolUtil;
import com.xyy.saas.payment.cores.bo.RefundBo;
import com.xyy.saas.payment.payment.core.dto.ActiveCodeDto;
import com.xyy.saas.payment.payment.core.dto.RefundDto;
import com.xyy.saas.payment.payment.core.dto.WithdrawalDto;
import com.xyy.saas.payment.payment.core.service.PaymentService;
import com.xyy.saas.payment.settle.core.service.AccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/** 常用接口 */
@RestController
@RequestMapping(value = {"/api/common"})
public class CommonController {
    private static final Logger LOGGER = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private AccountService accountService;

    @Autowired
    private AreaCodeManager areaCodeManager;

    @Autowired
    private PaymentService paymentService;

    /** 发送验证码 - 银行 */
    @RequestMapping(value = "/sentActiveCode", method = RequestMethod.POST)
    public String sentActiveCode(@RequestBody ActiveCodeDto activeCodeDto) {
        try {
            accountService.sendActiveCodeByPhoneNum(activeCodeDto);
        } catch (PaymentException e) {
            LOGGER.error("CommonController#sentActiveCode send active code is failed. e:{}", e);
        }
        return "SUCCESS.";
    }

    /** 通过地址查询地区编码 */
    @RequestMapping(value = "/areaCodeQuery", method = RequestMethod.GET)
    public String areaQuery(@RequestParam String address) {
        return areaCodeManager.getAreaCodeByArea(address);
    }

    /** 手动退款 */
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public String refund(@RequestBody RefundDto refundDto) {
        LOGGER.info("CommonController#refund start refundDto:{}", JSON.toJSONString(refundDto));
        try {
            RefundBo refundBo = paymentService.refund(refundDto);
            LOGGER.info("CommonController#refund end refundDto:{} refundBo:{}", JSON.toJSONString(refundDto), JSON.toJSONString(refundBo));
            return "OK.";
        } catch (Exception e) {
            LOGGER.error("CommonController#refund error refundDto:{}", JSON.toJSONString(refundDto), e);
        }
        return "FAIL";
    }

    /** 提现 */
    @RequestMapping(value = "/withdrawal", method = RequestMethod.POST)
    public String withdrawal(@RequestBody WithdrawalDto param) {
        try {
            return JSON.toJSONString(paymentService.withdraw(param));
        } catch (Exception e) {
            LOGGER.error("CommonController#withdrawal error. e:{}", e);
        }
        return "FAIL";
    }


    /**
     * 初始化京东采购金融渠道，交易数据，还款计划
     */
    @PostMapping(value = "/initJdCreditRepayPlan")
    public ResultVO initJdCreditRepayPlan(@RequestParam("minId") Long minId, @RequestParam("maxId") Long maxId){
        try {
            paymentService.initJdCreditRepayPlan(minId, maxId);
            return ResultVO.createSuccess();
        } catch (Exception e) {
            LOGGER.error("CommonController#initJdCreditRepayPlan error:{}", e);
        }
        return ResultVO.createError();
    }

    @GetMapping(value = "/alarmPool")
    public ResultVO alarmPoolTest() {
        for (int i = 0; i < 10; i++) {
            PoolUtil.ALARM_POOL.execute( NamedTask.createRun(()->{
                try {
                    Thread.sleep(10 * 1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }, "任务啦啦啦"));
        }
        return ResultVO.createSuccess();
    }
}
